import { TypedUseSelectorHook, useSelector, useDispatch } from 'react-redux'
import { combineReducers, configureStore } from '@reduxjs/toolkit'
import { persistStore, persistReducer } from 'redux-persist'
import storage from 'redux-persist/lib/storage'
import themeReducer from './features/themeSlice'
import userReducer from './features/userSlice'
import fileReducer from './features/fileSlice'
// 定义Redux Persist的配置
const persistConfig = {
  key: 'root',
  storage
  // 可以在这里指定你想要持久化的reducer，如果是全部则不用写
}
const rootReducer = combineReducers({
  theme: themeReducer,
  user: userReducer,
  file: fileReducer
})
// 使用persistReducer包装rootReducer
const persistedReducer = persistReducer(persistConfig, rootReducer)
// 创建store
const store = configureStore({
  reducer: persistedReducer,
  middleware: (getDefaultMiddleware) =>
    getDefaultMiddleware({
      serializableCheck: false
    })
  // 其他可能的store配置...
})
// 创建persistor
export const persistor = persistStore(store)

export type State = ReturnType<typeof store.getState>
export type Dispatch = typeof store.dispatch

export const useStoreSelector: TypedUseSelectorHook<State> = useSelector
export const useStoreDispatch = () => useDispatch<Dispatch>()

export default store
